package cn.edu.sjtu.software.sjtucontacts.activity;

import cn.edu.sjtu.software.sjtucontacts.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.*;

public class ImageCanvas extends View {

	private Bitmap myBitmap, background;
	private Bitmap enterIcon, phoneIcon, messageIcon, emailIcon, returnIcon;
	private Picture pic;
	private Paint myPaint, myBitmapPaint;
	private Canvas imageCanvas;
	private int weight, height;
	//The parameter view state stand for the state of the image canvas
	//0 represents for the origin size
	//1 represents for the big size
	private int viewstate = 0;

	private Animation mScaleAnimation = null;

	public ImageCanvas(Context context, int x, int y) {
		super(context);
		// this.setPadding(5, 0, 5, 0);

		myBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.icon);
		enterIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.profile_icon);
		phoneIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.dail_call_log_tel_out);
		messageIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.sms);
		emailIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.email);
		returnIcon = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.sym_feedback_icon);
		myBitmapPaint = new Paint();
		myPaint = new Paint();

		background = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
		imageCanvas = new Canvas(background);
		// setWillNotDraw(false);
		weight = x;
		height = y;
		/*
		 * this.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) {
		 * 
		 * clickTime = (clickTime + 1) % 2; invalidate(); } });
		 */
	}

	/*@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (x > fx && x < lx) {
				if (y > fy && y < ly) {
					clickTime = (clickTime + 1) % 2;
					if (clickTime == 1) {
						mScaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f,
								2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						mScaleAnimation.setDuration(500);
						this.startAnimation(mScaleAnimation);
					} else {
						mScaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f,
								0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						mScaleAnimation.setDuration(500);
						this.startAnimation(mScaleAnimation);
					}

					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (x > fx && x < lx) {
				if (y > fy && y < ly) {
					clickTime = (clickTime + 1) % 2;
					if (clickTime == 1) {
						mScaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f,
								2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						mScaleAnimation.setDuration(500);
						this.startAnimation(mScaleAnimation);
					} else {
						mScaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f,
								0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
								Animation.RELATIVE_TO_SELF, 0.5f);
						mScaleAnimation.setDuration(500);
						this.startAnimation(mScaleAnimation);
					}

					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			if (clickTime == 1) {
				if (x > 5 * weight / 6 && x < weight) {
					if (y > height / 6 && y < 7 * height / 18) {
						//parent.setText("Detail Infomation");
					} else if (y > 7 * height / 18 && y < 11 * height / 18) {
						//parent.setText("Call Me");
					}
				} else {
					//parent.setText("Welcome");
				}
			} else {
				//parent.setText("Welcome");
			}
			break;
		}
		return true;
	}*/
	
	public void magnifyImage() {
		mScaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f,
				2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		mScaleAnimation.setDuration(500);
		this.startAnimation(mScaleAnimation);
		viewstate = 1;
	}
	
	public void lessenImage() {
		mScaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		mScaleAnimation.setDuration(500);
		this.startAnimation(mScaleAnimation);
		viewstate = 0;
	}
	
	public int getViewState() {
		return viewstate;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.drawColor(Color.BLACK);
		//if (viewstate == 0) {

			canvas.drawBitmap(myBitmap, new Rect(0, 0, myBitmap.getWidth(),
					myBitmap.getHeight()), new Rect(0, 0,
					weight, height), myPaint);
			//fx = weight / 3;
			//fy = height / 3;
			//lx = 2 * weight / 3;
			//ly = 2 * height / 3;
		//} else {

/*			canvas.drawBitmap(myBitmap, new Rect(0, 0, myBitmap.getWidth(),
					myBitmap.getHeight()), new Rect(weight / 6, height / 6,
					5 * weight / 6, 5 * height / 6), myPaint);
			canvas.drawBitmap(enterIcon, new Rect(0, 0, enterIcon.getWidth(),
					enterIcon.getHeight()), new Rect(5 * weight / 6,
					height / 6, weight, 7 * height / 18), myPaint);
			canvas.drawBitmap(phoneIcon, new Rect(0, 0, phoneIcon.getWidth(),
					phoneIcon.getHeight()), new Rect(5 * weight / 6,
					7 * height / 18, weight, 11 * height / 18), myPaint);*/
			//fx = weight / 6;
			//fy = height / 6;
			//lx = 5 * weight / 6;
			//ly = 5 * height / 6;
		//}
		// imageCanvas.drawBitmap(myBitmap, 0, 0, myBitmapPaint);
		// canvas.scale((float)0.002, (float)0.002);
		// drawImage(canvas);
	}

	public void drawImage(Canvas canvas) {
		// myBitmap =
		// ((BitmapDrawable)this.getResources().getDrawable(R.drawable.sms)).getBitmap();

	}

}
